package filter1;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import java.util.Map;

@WebFilter(filterName = "filter1.Filter1")
public class Filter1 implements Filter {
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req1=(HttpServletRequest)req;
        HttpServletResponse res1=(HttpServletResponse)resp;
        Map<String,String> login = (Map<String, String>) req1.getSession().getAttribute("Login");
       String token=req1.getParameter("token");
        if(token!=null){
            chain.doFilter(req, resp);
        }
        else if (login== null) {
            res1.sendRedirect("https://cas.dgut.edu.cn?appid=javaee&state=STATE");
        }
        else {
            res1.getWriter().write("已登录，请勿重复登录");
            res1.sendRedirect("/index.jsp");
        }

    }

    public void init(FilterConfig config) throws ServletException {

    }

}
